//push events on a stack
//will automatically be reportet to whatever is set in the options
/*Looks up for following Options
	event_stack_length: number of events before sending them to server
*/
(function (that) {

	//local Link to Global-Object or throw Exception
	if(!that){throw "Global object missing, bitchesss";}
	var that = that;

	//Get reference of global options
	var options = that.get_global_options()

	//Array to save events
	, event_array = []

	//Start time
	, start = new Date().getTime()

	//boolean to check if first time being called
	, first = true

	//Page-ID not sure why here :P
	, page_id = 1234567890

	//Get cookies if set
	, cookie = that.identify(page_id);




	//function to send data and reset the event_array;
	var send_data = function(params){

		params = params||{};

		var default_params = {
			u:	cookie.user,
			p:	cookie.page,
			d:	event_array.join('|'),
			sn:	options.cookie.random_session_id,
			my: options.max_y_offset
		};

		sauberfant.extend(params, default_params);

		that.event_send_data(params);

		event_array = [];
	};




	that.event_stack = function (type, locX, locY){

		if(!options.is_float_layout && !!options.container_width){
			var over_flow = options.browser_width - options.container_width;
			if(over_flow > 0){
				locX = locX - (over_flow/2);
			}
		}

		//minify positions by dividing by 8 and using base of 36 (maximum allowed)
		locX = (locX >> 3).toString(36);
		locY = (locY >> 3).toString(36);

		var offset = Math.round((new Date().getTime() - start)/50).toString(36);

		event_array.push([type, locX, locY, offset].join(","));

		if (first && event_array.length >= that.options.first_event_stack_length) {

			first = false;

			send_data({
				bw: that.browser_width,
				bh: that.browser_height,
				ua: navigator.userAgent,
				st: Math.round(start/50),
				cc: that.get_cookie(page_id),
				cv: that.options.canvas
			});

		} else if (event_array.length >= that.options.event_stack_length) {
			send_data();
		}
	}


	that.event_stack_flush = function(){
		send_data();
	}


})(sauberfant);
